package anc.tankshooter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.andengine.engine.Engine;
import org.andengine.engine.FixedStepEngine;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.WakeLockOptions;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.ui.activity.BaseGameActivity;

import anc.tankshooter.database.DatabaseQuery;
import anc.tankshooter.map.GameMap;
import anc.tankshooter.network.GameClient;
import anc.tankshooter.ui.IScreen;
import anc.tankshooter.ui.ResourcesManager;
import anc.tankshooter.ui.SceneManager;
import anc.tankshooter.ui.SplashScreen;
import android.os.Bundle;
import android.view.KeyEvent;


public class GameActivity extends BaseGameActivity {

	public static String DATABASE_FOLDER;
	public static final String SETTING_PATH="anc.tankshooter.ini";
	public static final int CAMERA_WITDH=800;
	public static final int CAMERA_HEIGHT=480;
	
	public static GameClient mClient;
	public static GameSetting mGlobalSetting;
	public static GameMap mGlobalMap;
	public static DatabaseQuery mDatabase;
	
	private Camera mCamera;
	private Engine mEngine;
	
	@Override
	protected void onCreate(Bundle pSavedInstanceState) {
		super.onCreate(pSavedInstanceState);
	
		mDatabase = new DatabaseQuery(this);
		mClient = new GameClient();
		DATABASE_FOLDER = "data/data/"+this.getPackageName()+"/databases/";
		File f = new File(DATABASE_FOLDER);
		if(!f.exists()) f.mkdirs();
		GameActivity.mGlobalSetting = readSetting("data/data/"+this.getPackageName()+"/databases/");
		if(GameActivity.mGlobalSetting==null)
			GameActivity.mGlobalSetting=new GameSetting();
		
		GameActivity.mClient.startClient();
	}
	
	@Override
	protected void onStop() {
		saveSetting("data/data/"+this.getPackageName()+"/databases/");
		super.onStop();
	}
	public static boolean loadLevelSetting(int level){
		
		boolean isSuccess = false;
		
		GameActivity.mGlobalSetting.mOutLocation.clear();
		GameActivity.mGlobalSetting.setCurrentLevel(level);
		if(GameActivity.mGlobalMap.loadMap(GameActivity.mGlobalSetting.getCurrentLevel())) {
			
			int[] location1 = null;
			int[] location2 = null;
			int[] location3 = null;
			switch (GameActivity.mGlobalSetting.getCurrentLevel()) {
			case 1:
				GameActivity.mGlobalSetting.mNormalTank =25;
				GameActivity.mGlobalSetting.mFastTank = 5;
				GameActivity.mGlobalSetting.mHeavyTank = 2;
				GameActivity.mGlobalSetting.mBossTank = 0;
				location1 =new int[]{6*32,11*32};
				location2 =new int[]{24*32,9*32};
				location3 =new int[]{20*32,0};
				
				GameActivity.mGlobalSetting.mOutLocation.add(location1);
				GameActivity.mGlobalSetting.mOutLocation.add(location2);
				GameActivity.mGlobalSetting.mOutLocation.add(location3);
				
				isSuccess = true;
				break;
			case 2:
				GameActivity.mGlobalSetting.mNormalTank = 25;
				GameActivity.mGlobalSetting.mFastTank = 15;
				GameActivity.mGlobalSetting.mHeavyTank = 10;
				GameActivity.mGlobalSetting.mBossTank = 1;
				location1 =new int[]{9*32,11*32};
				location2 =new int[]{24*32,10*32};
				location3 =new int[]{17*32,32};
				
				GameActivity.mGlobalSetting.mOutLocation.add(location1);
				GameActivity.mGlobalSetting.mOutLocation.add(location2);
				GameActivity.mGlobalSetting.mOutLocation.add(location3);
				isSuccess = true;
				break;
			case 3:
				GameActivity.mGlobalSetting.mNormalTank = 30;
				GameActivity.mGlobalSetting.mFastTank = 15;
				GameActivity.mGlobalSetting.mHeavyTank = 20;
				GameActivity.mGlobalSetting.mBossTank = 2;
				location1 =new int[]{12*32,6*32};
				location2 =new int[]{15*32,0*32};
				location3 =new int[]{20*32,8*32};
				
				GameActivity.mGlobalSetting.mOutLocation.add(location1);
				GameActivity.mGlobalSetting.mOutLocation.add(location2);
				GameActivity.mGlobalSetting.mOutLocation.add(location3);
				isSuccess = true;
				break;
			default:
				break;
			}
		}
		return isSuccess;
	}
	
	
	@Override
	public EngineOptions onCreateEngineOptions() {
		mCamera= new Camera(0, 0,CAMERA_WITDH, CAMERA_HEIGHT);
		EngineOptions engineOption = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, 
										new RatioResolutionPolicy(CAMERA_WITDH,CAMERA_HEIGHT),
										mCamera);
		engineOption.getAudioOptions().setNeedsMusic(true).setNeedsSound(true);
	    engineOption.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
		mEngine=new FixedStepEngine(engineOption,25);
		
		return engineOption;
	}

	@Override
	public void onCreateResources(OnCreateResourcesCallback callback)
			throws Exception {
		ResourcesManager.prepareManager(mEngine, this,mCamera,mEngine.getVertexBufferObjectManager());
		ResourcesManager.getInstance().loadGlobalResources();
		GameActivity.mGlobalMap=new GameMap(this.getAssets(), this.getTextureManager(), this.getVertexBufferObjectManager());
		callback.onCreateResourcesFinished();
	}

	@Override
	public void onCreateScene(OnCreateSceneCallback callback) throws Exception {
		SceneManager.getInstance().setCallback(callback);
		
		ResourcesManager.getInstance().loadSplashScreen();
		SplashScreen screen = new SplashScreen();
		SceneManager.getInstance().setScene(screen);
	}

	@Override
	public void onPopulateScene(Scene screen, OnPopulateSceneCallback callback)
			throws Exception {
		
	    callback.onPopulateSceneFinished();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		IScreen screen = SceneManager.getInstance().getCurrentScene();
		if(screen!=null && keyCode == KeyEvent.KEYCODE_BACK)
			return screen.onBackKeyPressed();
		return super.onKeyDown(keyCode, event);
	}
	public static GameSetting readSetting(String folder){
		ObjectInputStream stream = null;
		GameSetting setting = null;
		try{
			stream = new ObjectInputStream(new FileInputStream(folder+GameActivity.SETTING_PATH));
			setting = (GameSetting)stream.readObject();
			stream.close();
		}catch(Exception ex){
			ex.getMessage();
		}
		return setting;
	}
	public static void saveSetting(String folder){
		ObjectOutputStream stream = null;
		try{
			stream = new ObjectOutputStream(new FileOutputStream(folder+GameActivity.SETTING_PATH,false));
			stream.writeObject(GameActivity.mGlobalSetting);
			stream.close();
		}catch(Exception ex){
			ex.getMessage();
		}
	}
}
